package com.apexedu.theme.service.Impl;

import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.Cn2Spell;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.theme.action.AucThemeSiteManageAction;
import com.apexedu.theme.entity.TXxfbSite;
import com.apexedu.util.StringUtil;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 门户站点管理
 * Created by 建 on 2015/4/7.
 * 对门户站点的新增，删除，修改等操作
 */
@Service("aucThemeSiteManageService")
public class AucThemeSiteManageServiceImpl extends AbstractBaseServiceSupport{
    @Override
    public void saveOrUpdate(Object o) throws Exception {
        AucThemeSiteManageAction action=(AucThemeSiteManageAction)o;
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TXxfbSite xxfbSite=action.getXxfbSite();
        if(null!=xxfbSite){
            if(StringUtil.isNotBlank(xxfbSite.getWid())){//更新
                baseDao.updateNotNull(xxfbSite);
            }else{//保存
                if(StringUtil.isBlank(xxfbSite.getZdbm())){//如果站点编码为空
                    String py=Cn2Spell.getPYString(xxfbSite.getZdmc());
                    long num=baseDao.count("select count(*) as c from TXxfbSite t where t.zdmb = ? ",py);
                    while (num>0){
                        py=py+"_a";
                        num=baseDao.count("select count(*) as c from TXxfbSite t where t.zdmb = ? ",py);
                    }
                    xxfbSite.setZdbm(py);
                }
                xxfbSite.setCjsj(format.parse(format.format(new Date())));
                baseDao.save(xxfbSite);
            }
        }
    }

    @Override
    public void load(Object o) throws Exception {
        AucThemeSiteManageAction action=(AucThemeSiteManageAction)o;
        String wid=action.getWid();
        if(StringUtil.isNotBlank(wid)){
            TXxfbSite xxfbSite=baseDao.findById(TXxfbSite.class,wid);
            action.setXxfbSite(xxfbSite);
        }
    }

    @Override
    public List<?> list(Object o, Pager pager) throws Exception {
        String hql=" from TXxfbSite t where t.state = '1' order by t.ordernum desc ";
        String hql_="select new TXxfbSite (t.wid,t.zdmc,t.zdbm,(select s.zdmc from TSysCode s where s.zdlb='qyyf' and s.zdbm =t.state )as state,t.cjsj)";
        long c=baseDao.count("select count(*) as c "+hql);
        pager.setTotalRows(c);
        List<TXxfbSite> list=baseDao.findPageByHql(hql_+hql,pager);
        return list;
    }
    @Override
    public boolean remove(Object o) throws Exception {
        AucThemeSiteManageAction action=(AucThemeSiteManageAction)o;
        String wid=action.getWid();
        if(StringUtil.isNotBlank(wid)){
            String[] ids=wid.split(",");
            for (String id:ids){
                String sql_delete_zt_zd="delete from t_xxfb_site_zt t where t.siteid = ? ";
                DBUtil.executeSQL(sql_delete_zt_zd,id);//删除站点主题关系
            }
            baseDao.deleteAll("TXxfbSite","wid","=",wid);
        }
        return false;
    }
}
